clear all
set more off

// Robustness commute (mergers, epci, large mkt)

global input "...\replication\data"
global output "...\replication\outputs"

****************************************
****************************************

use "$input\com_data.dta", clear

****************** Costmetics ******************
** Reomove Corsica
ren com_res com
drop if substr(com,2,1) == "A"
drop if substr(com,2,1) == "B"
destring com, replace

****************** Get dist border ******************
merge m:1 com using "$input\com_arcgis.dta"
drop if _merge != 3
drop _merge
keep if dist_nat_border < 0 


merge m:1 com using "$input\com_mergers.dta"
drop if _merge == 2
drop _merge
replace mergers = 0 if mergers == .

****************** Get main vars******************
** Outcome
egen sum_ind = sum(ind), by(com)
gen p_ind = ind/sum_ind
** Treatment
gen Treat = 0
replace Treat = 1 if same == -1

drop if dist_border == 0

keep if abs(dist_border) <= 25


destring dep_res dep_work, replace


//////////////////////////////////////////////////////////
//////////////// Excluding mergers ///////////////////////

****************** calonico et al ******************
rdbwselect p_ind dist_border, c(0) 
local opt_h=e(h_mserd)

rdrobust p_ind dist_border if mergers == 0,c(0) h(`opt_h') 
sca N_bw=e(N_h_l)+e(N_h_r)
estadd scalar N_bw=N_bw
sum p_ind if abs(dist_border) < `opt_h' & dist_border < 0
sca m_outc = r(mean)
estadd scalar m_outc=m_outc
est store ATE_1

rdrobust p_ind dist_border if mergers == 0,c(0) h(`opt_h') covs(dep_res dep_work)
sca N_bw=e(N_h_l)+e(N_h_r)
estadd scalar N_bw=N_bw
sum p_ind if abs(dist_border) < `opt_h' & dist_border < 0
sca m_outc = r(mean)
estadd scalar m_outc=m_outc
est store ATE_2

rdrobust p_ind dist_border if mergers == 0,c(0) h(2*`opt_h') covs(dep_res dep_work)
sca N_bw=e(N_h_l)+e(N_h_r)
estadd scalar N_bw=N_bw
sum p_ind if abs(dist_border) < 2*`opt_h' & dist_border < 0
sca m_outc = r(mean)
estadd scalar m_outc=m_outc
est store ATE_3

rdrobust p_ind dist_border if mergers == 0,c(0) h(0.5*`opt_h') covs(dep_res dep_work)
sca N_bw=e(N_h_l)+e(N_h_r)
estadd scalar N_bw=N_bw
sum p_ind if abs(dist_border) < 0.5*`opt_h' & dist_border < 0
sca m_outc = r(mean)
estadd scalar m_outc=m_outc
est store ATE_4

****************** Results in Table ******************
esttab ATE_1 ATE_2 ATE_3 ATE_4, tex ///
b(%9.3f) se(%9.3f) stats( h_l N N_bw m_outc, labels("Bandwidth" "Obs" "Obs. bw." "Mean dep. var. below" ) ///
fmt( %9.3gc %9.0gc %9.0gc %9.3gc)) nolines nogaps star(* 0.10 ** 0.05 *** 0.01) ///
keep(RD_Estimate) coef(RD\_Estimate  "ATE") 


//////////////////////////////////////////////////////////
//////////////// Excluding large labor markets ///////////////////////

gen large_mkt = 0
replace large_mkt = 1 if dep_res == 75
replace large_mkt = 1 if dep_work == 75
replace large_mkt = 1 if dep_res == 69
replace large_mkt = 1 if dep_work == 69
replace large_mkt = 1 if dep_res == 13
replace large_mkt = 1 if dep_work == 13
 

****************** calonico et al ******************
rdbwselect p_ind dist_border, c(0) 
local opt_h=e(h_mserd)

rdrobust p_ind dist_border if large_mkt == 0,c(0) h(`opt_h') 
sca N_bw=e(N_h_l)+e(N_h_r)
estadd scalar N_bw=N_bw
sum p_ind if abs(dist_border) < `opt_h' & dist_border < 0
sca m_outc = r(mean)
estadd scalar m_outc=m_outc
est store ATE_1

rdrobust p_ind dist_border if large_mkt == 0,c(0) h(`opt_h') covs(dep_res dep_work)
sca N_bw=e(N_h_l)+e(N_h_r)
estadd scalar N_bw=N_bw
sum p_ind if abs(dist_border) < `opt_h' & dist_border < 0
sca m_outc = r(mean)
estadd scalar m_outc=m_outc
est store ATE_2

rdrobust p_ind dist_border if large_mkt == 0,c(0) h(2*`opt_h') covs(dep_res dep_work)
sca N_bw=e(N_h_l)+e(N_h_r)
estadd scalar N_bw=N_bw
sum p_ind if abs(dist_border) < 2*`opt_h' & dist_border < 0
sca m_outc = r(mean)
estadd scalar m_outc=m_outc
est store ATE_3

rdrobust p_ind dist_border if large_mkt == 0,c(0) h(0.5*`opt_h') covs(dep_res dep_work)
sca N_bw=e(N_h_l)+e(N_h_r)
estadd scalar N_bw=N_bw
sum p_ind if abs(dist_border) < 0.5*`opt_h' & dist_border < 0
sca m_outc = r(mean)
estadd scalar m_outc=m_outc
est store ATE_4

****************** Results in Table ******************
esttab ATE_1 ATE_2 ATE_3 ATE_4, tex ///
b(%9.3f) se(%9.3f) stats( h_l N N_bw m_outc, labels("Bandwidth" "Obs" "Obs. bw." "Mean dep. var. below" ) ///
fmt( %9.3gc %9.0gc %9.0gc %9.3gc)) nolines nogaps star(* 0.10 ** 0.05 *** 0.01) ///
keep(RD_Estimate) coef(RD\_Estimate  "ATE") 


****************************************
****************************************

use "$input\com_data.dta", clear

****************** Costmetics ******************
** Reomove Corsica
ren com_res com
drop if substr(com,2,1) == "A"
drop if substr(com,2,1) == "B"
destring com, replace

****************** Get dist border ******************
merge m:1 com using "$input\com_arcgis.dta"
drop if _merge != 3
drop _merge
** Drop those within 30km of coast or national border
keep if dist_nat_border < 0 


merge m:1 com using "$input\com_epci.dta"
drop if _merge == 2
drop _merge
replace epci = 0 if epci == .

****************** Get main vars******************
** Outcome
egen sum_ind = sum(ind), by(com)
gen p_ind = ind/sum_ind
** Treatment
gen Treat = 0
replace Treat = 1 if same == -1

drop if dist_border == 0

keep if abs(dist_border) <= 25


destring dep_res dep_work, replace


//////////////////////////////////////////////////////////
//////////////// Excluding epci ///////////////////////

****************** calonico et al ******************
rdbwselect p_ind dist_border if epci == 0, c(0) 
local opt_h=e(h_mserd)

rdrobust p_ind dist_border if epci == 0,c(0) h(`opt_h') 
sca N_bw=e(N_h_l)+e(N_h_r)
estadd scalar N_bw=N_bw
sum p_ind if abs(dist_border) < `opt_h' & dist_border < 0
sca m_outc = r(mean)
estadd scalar m_outc=m_outc
est store ATE_1

rdrobust p_ind dist_border if epci == 0,c(0) h(`opt_h') covs(dep_res dep_work)
sca N_bw=e(N_h_l)+e(N_h_r)
estadd scalar N_bw=N_bw
sum p_ind if abs(dist_border) < `opt_h' & dist_border < 0
sca m_outc = r(mean)
estadd scalar m_outc=m_outc
est store ATE_2

rdrobust p_ind dist_border if epci == 0,c(0) h(2*`opt_h') covs(dep_res dep_work)
sca N_bw=e(N_h_l)+e(N_h_r)
estadd scalar N_bw=N_bw
sum p_ind if abs(dist_border) < 2*`opt_h' & dist_border < 0
sca m_outc = r(mean)
estadd scalar m_outc=m_outc
est store ATE_3

rdrobust p_ind dist_border if epci == 0,c(0) h(0.5*`opt_h') covs(dep_res dep_work)
sca N_bw=e(N_h_l)+e(N_h_r)
estadd scalar N_bw=N_bw
sum p_ind if abs(dist_border) < 0.5*`opt_h' & dist_border < 0
sca m_outc = r(mean)
estadd scalar m_outc=m_outc
est store ATE_4

****************** Results in Table ******************
esttab ATE_1 ATE_2 ATE_3 ATE_4, tex ///
b(%9.3f) se(%9.3f) stats( h_l N N_bw m_outc, labels("Bandwidth" "Obs" "Obs. bw." "Mean dep. var. below" ) ///
fmt( %9.3gc %9.0gc %9.0gc %9.3gc)) nolines nogaps star(* 0.10 ** 0.05 *** 0.01) ///
keep(RD_Estimate) coef(RD\_Estimate  "ATE") 
